﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Huazhu.PMS.Framework.Organization
{
    public class SubNodeCollection
    {
        private OrgTreeNode _rootNode;

        public const string ALL = "all";

        internal SubNodeCollection(OrgTreeNode rootNode)
        {
            _rootNode = rootNode;
        }

        public IList<OrgTreeNode> this[string nodeType]
        {
            get
            {
                if (nodeType == null)
                    return null;
                IList<OrgTreeNode> results = new List<OrgTreeNode>();
                GetSubNodes(_rootNode, nodeType, ref results);
                return results;
            }
        }

        private void GetSubNodes(OrgTreeNode node, string nodeType, ref IList<OrgTreeNode> nodes)
        {
            foreach (KeyValuePair<string, OrgTreeNode> hit in node.ChildNodes)
            {
                if (nodeType.Equals(ALL) || nodeType.Equals(hit.Value.NodeType))
                {
                    if (nodes == null)
                    {
                        nodes = new List<OrgTreeNode>();
                    }
                    nodes.Add(hit.Value);
                }
                GetSubNodes(hit.Value, nodeType, ref nodes);
            }
        }
    }
}
